//
//  skillViewController.m
//  LOL_PalmarHelper
//
//  Created by dlios on 15-6-26.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "skillViewController.h"

@interface skillViewController ()

@end

@implementation skillViewController

-(void)dealloc
{
    
    [_array release];
    [_myArray release];
    [_skillModel release];
    [_collectionV release];
    
    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title =@"召唤师技能列表";

    
//    [self setAutomaticallyAdjustsScrollViewInsets:YES];
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize = CGSizeMake((self.view.bounds.size.width - 90) / 3, (self.view.bounds.size.width - 90) / 3);
    flowlayout.sectionInset = UIEdgeInsetsMake(5, 5, 40,5);
    flowlayout.minimumInteritemSpacing = 10;
    flowlayout.minimumLineSpacing = 40;
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height - 115) collectionViewLayout:flowlayout];
 
//    NSLog(@"----------%f", self.view.frame.size.height+self.navigationController.navigationBar.bounds.size.height + self.navigationController.tabBarController.tabBar.bounds.size.height - 150);
    self.collectionV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionV];
//    self.collectionV.bounces = YES;
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    
    //    注册
    [self.collectionV registerClass:[SkillCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    

   
    [AFN AFNGetData:@"http://lolbox.duowan.com/phone/apiSumAbility.php?v=108&OSType=iOS8.3" andBlock:^(id respondes) {
        
        
        if (![respondes isKindOfClass:[@"error" class]]){
        
        
         self.array = [NSMutableArray array];
        
        NSMutableArray *array = respondes;
        
        for (NSMutableDictionary *dic  in array) {
            self.skillModel = [[skill alloc] init];
            self.skillModel.name = [dic objectForKey:@"name"];
             self.skillModel.myId = [dic objectForKey:@"id"];
             self.skillModel.level = [dic objectForKey:@"level"];
             self.skillModel.cooldown = [dic objectForKey:@"cooldown"];
             self.skillModel.des = [dic objectForKey:@"des"];
             self.skillModel.strong = [dic objectForKey:@"strong"];
             self.skillModel.tips = [dic objectForKey:@"tips"];
            [self.array addObject:self.skillModel];

        }
       
        [self.collectionV reloadData];
            
        }else if ([respondes isKindOfClass:[@"error" class]]) {
            
            
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:al];
            [al show];
            [al release];
            
            
        }
       
            
    }];
   

    
    self.myArray = [NSMutableArray arrayWithObjects:@"image1",@"image2",@"image8",@"image3",@"image4",@"image5",@"image6",@"image7",@"image9",@"image10",@"image11",@"image12",@"image13", nil];
    
    
   
    
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
   
    skillDetaViewController *skillD = [[skillDetaViewController alloc] init];

    [self.navigationController pushViewController:skillD animated:YES];

    
    skillD.mySkill = [self.array objectAtIndex:indexPath.row];
    
    
    
    skillD.mySkill.imageName = [self.myArray objectAtIndex:indexPath.row];
    
    

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    
    
  
    return self.array.count;
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    //直接从系统的重用池里取cell
   SkillCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor= [UIColor purpleColor];
    
    skill *skillModel = [self.array objectAtIndex:indexPath.row];
    cell.model = skillModel;
    
    cell.str = [self.myArray objectAtIndex:indexPath.row];
    
    return cell;
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
